var myApp = sy.getModule('myApp',[]);
myApp.directive('onFinishRenderFilters', function ($timeout) {
    return {
        restrict: 'A',
        link: function(scope, element, attr) {
            if (scope.$last === true) {
                $timeout(function() {
                    scope.$emit('ngRepeatFinished',attr.onFinishRenderFilters);
                });
            }
        }
    };
});
myApp.controller('creditController', function($scope,$http) {
  $scope.cates = [];
  $scope.cateCode = '';
  $scope.storeList = [];
  $scope.busy = false;
  $scope.page = 1;
  $scope.limit = 10;
  $scope.hasMore = true;
  $scope.login = false;
  $scope.user = {};
  sy.openLoad();
  $http.post("/m/credit/cate",{})
		.success(function (data) {
			$scope.cates = data;
			if (data){
				$scope.store("");
			}
            sy.closeLoad();
        }).error(function(data, status) {
        	sy.closeLoad();
		});
  $http.post("/m/credit/credit",{})
	.success(function (data) {
		$scope.cates = data;
		if (data){
			$scope.login = true;
			$scope.user = data;
		}
      sy.closeLoad();
  }).error(function(data, status) {
  	sy.closeLoad();
	});
  
  $scope.nextPage = function() {
	  if ($scope.busy || !$scope.hasMore) return;
	  $scope.busy = true;
      sy.openLoad();
      $http.post("/m/credit/goods",{
    	  		page:$scope.page,limit:$scope.limit,
    	  		code:$scope.cateCode
      		})
			.success(function (data) {
				if(data.length < $scope.limit)$scope.hasMore = false;
				$.each(data.content,function(i,o){
					console.log(o)
					$scope.storeList.push(o);
				});
				$scope.busy = false;
				$scope.page += 1;
	            sy.closeLoad();
	        }).error(function(data, status) {
	        	sy.closeLoad();
			});
  };
  
  $scope.creditDetail = function(id){
	  location.href='/m/credit/'+id;
  };
  
  $scope.store = function(cate){
	  if(cate==0) $scope.cateCode = '';
	  else $scope.cateCode = cate;
	  
	  $scope.busy = false;
	  $scope.page = 1;
	  $scope.limit = 10;
	  $scope.hasMore = true;
	  $scope.storeList = [];
	  $scope.nextPage();
  };
  
  $scope.$on('ngRepeatFinished', function (event,data) {
		if(data == 'cate'){
			var initNavSlider = function(sliderContainerId){
				var navSwiper = new Swiper('#' + sliderContainerId,{
					pagination: '',
					createPagination: false,
					paginationClickable: false,
					slidesPerView: 'auto'
				})
			}
		
			initNavSlider('navSlider');//初始化分类导航触屏滑动
		}
	});
  
  $scope.storeDetail = function(id){
	location.href = '/m/s/'+id;  
  };
  
});
